import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_yd_simple/Project/Home/View/HomeCarouselImgView.dart';
import 'package:flutter_yd_simple/Project/Home/View/HomeItemView.dart';
import 'package:flutter_yd_simple/Project/ScanModule/DistributeScan/DistributeScanPage.dart';
import 'package:flutter_yd_simple/Project/ScanModule/SignScan/SignScanPage.dart';
import 'package:flutter_yd_simple/Project/SignOrder/SignBaseOrder.dart';
import 'package:fluttertoast/fluttertoast.dart';
//import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});


  @override
  State<HomePage> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<HomePage> {
  var textStr = "";
  TextEditingController _editingController = new TextEditingController();
  var itemNum = [{"title":"待签收","num":"0"},
                {"title":"应到未分","num":"1"},
                {"title":"问题件","num":"2"},
                {"title":"已签收","num":"3"},
                {"title":"待揽收","num":"4"},
                {"title":"预约单","num":"5"},
                {"title":"代收款","num":"6"},
                {"title":"收件跟踪","num":"7"}];
  var showItemNum = [{"title":"派前电联","num":"0"},
                {"title":"送货上门","num":"1"},
                {"title":"投诉预警","num":"2"},
                {"title":"客户投诉","num":"3"}];

     Widget _listItemBuilder(BuildContext content, int index) {
      return Container(
        height: 35,
        //color: Color(0xFF2196F3),
        // margin: EdgeInsets.all(5.0),
        // padding: EdgeInsets.all(10.0),
        child:Row(
          children: [
            const SizedBox(width: 10),
            Image.asset("lib/Project/Images/Home/home_messages.png"),
            const SizedBox(width: 10),
            Text('$index 寒冬还在持续，小哥保险为出行添保护',style: const TextStyle(color: Color(0xFF222222),fontSize: 12)),
          ],
        )
      );
    }

  @override
  Widget build(BuildContext context) {
    
    
     return Scaffold(
      backgroundColor: const Color(0xFFF6F8FA),
      
      
      appBar: AppBar(
        title: const Text('首页'),
      ),
      
      //appBar: null,
      body: SingleChildScrollView(
        child: Column(
        children: [
          Text("输入的内容$textStr"),
          const SizedBox(height: 4),

          //搜索全部
          Container(
            color: Colors.orange,
            height: 48,
            width: MediaQuery.of(context).size.width,
            alignment: Alignment.center,
            child: Row(
              mainAxisSize: MainAxisSize.min,
            
              children: [
                const SizedBox(width: 12),
                const Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text('上海',style: TextStyle(fontSize: 13,color: Colors.white)),
                    Text('晴 8°C',style: TextStyle(fontSize: 10,color: Colors.white))
                  ],
                ),
                const SizedBox(width: 6),
                GestureDetector(
                  child: Image.asset("lib/Project/Images/Home/home_page_left.png",width: 18,height: 18),
                  onTap: () {
                    Fluttertoast.showToast(msg: "点击了二维码icon",gravity:ToastGravity.CENTER);
                  },
                ),
                
                const SizedBox(width: 6),
                //搜索输入框
                Expanded(child: Container(
                  height: 32,
                  decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(16)),color: Colors.white),
                  child: TextField(
                    controller: _editingController,
                    keyboardType: TextInputType.number,
                    maxLines: 1,
                    style: const TextStyle(fontSize: 12),
                    decoration: const InputDecoration(
                      hintText: '输入运单号查询',
                      prefixIcon: Icon(Icons.search),
                      border: InputBorder.none,
                      contentPadding: EdgeInsets.only(bottom: -3), //这里是关键
                    ),
                    inputFormatters: <TextInputFormatter>[
                      LengthLimitingTextInputFormatter(20)//限制长度
                    ],
                    onChanged: (value) {
                      if(value.length > 5) {
                        Fluttertoast.showToast(msg: "运单号过长",gravity:ToastGravity.CENTER);
                        _editingController.text = value.substring(0,5);
                      }
                      this.setState(() {
                          textStr = _editingController.text;
                      });

                      print("value $_editingController.text");
                    },
                  ),
                )),

                const SizedBox(width: 6),
                GestureDetector(
                  child: Image.asset("lib/Project/Images/Home/home_right_scan.png",width: 18,height: 18),
                  onTap: () {
                    Fluttertoast.showToast(msg: "点击了扫描icon",gravity:ToastGravity.CENTER);
                  },
                ),
                const SizedBox(width: 6),
                GestureDetector(
                  child: Image.asset("lib/Project/Images/Home/home_right_message.png",width: 18,height: 18),
                  onTap: () {
                    Fluttertoast.showToast(msg: "点击了消息聊天icon",gravity:ToastGravity.CENTER);
                  },
                ),
                const SizedBox(width: 6),

              ],
            ),

          ),
          
           //签收 数量
          Container(
            height: 148,
            decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(12)),color: Colors.white),
            margin: const EdgeInsets.only(top: 8,left: 12,right: 12,bottom: 8),
            padding: const EdgeInsets.only(top: 8,bottom: 8),
            child: GridView.count(
              physics: const NeverScrollableScrollPhysics(),//设置禁止滚动
              crossAxisCount: 4,
              childAspectRatio: 1.2,// 宽高比
              children: List.generate(itemNum.length, (index) {
                return GestureDetector(
                  onTap: () {
                    disposeItemClickAction(itemNum[index]["title"]!,context);
                  },
                  child: Column(
                    children: [
                      Text(itemNum[index]["num"]!,style: const TextStyle(color: Color(0xFF222222),fontSize: 26,fontWeight: FontWeight.w400)),
                      Text(itemNum[index]["title"]!,style: const TextStyle(color: Color(0xFF222222),fontSize: 12))
                    ],
                  )
                );
              })
            ),
          ),

          //众多 滚动 item
          Container(
            height: 148,
            //color: Colors.orange,
            margin: const EdgeInsets.only(left: 13,right: 13),
            child:  HomeItemView()
          ),
         
          //广播
          Container(
            height: 35,
            margin: const EdgeInsets.only(top: 0,left: 12,right: 12,bottom: 0),
            padding: const EdgeInsets.only(left: 12,right: 12),
            decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(8)),color: Colors.white),
            child: ListView.separated(
                itemBuilder: _listItemBuilder,
                itemCount: 10,
                separatorBuilder: (BuildContext ctx,int index) {
                  return const Divider(color: Colors.white,height: 0,indent: 60,endIndent: 10,thickness: 0);
               },
            ),
          ),

          //派前电联
          Container(
            margin: const EdgeInsets.only(top: 8,left: 12,right: 12),
            decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(12)),color: Colors.white),
            height: 66,
            child: Row(
              children: <Widget>[
                for (int i = 0; i < (showItemNum.length ?? 0); i++) 
                  Container(
                    width: (MediaQuery.of(context).size.width - 24)/showItemNum.length,
                    child: Column(
                      children:[
                        const SizedBox(height: 12),
                        Text(showItemNum[i]["num"]!,style: const TextStyle(fontSize: 18,color: Color(0xFF222222),fontWeight: FontWeight.w400)),
                        Text(showItemNum[i]["title"]!,style: const TextStyle(fontSize: 12,color: Color(0xFF222222))),
                      ],
                    ),
                  ),
              ]
            ),
          ),

          //待办
          Container(
            height: 46,
            margin: const EdgeInsets.only(top: 8,left: 12,right: 12,bottom: 8),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                for (int i = 0; i < 2; i++) 
                
                Container(
                  width: (MediaQuery.of(context).size.width - 12*3)/2,
                  decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(12)),color: Colors.white),
                  height: 46,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child:Row(
                          children: [
                            const SizedBox(width: 12),
                            Image.asset(i==0?"lib/Project/Images/Home/home_todo_Icon.png":"lib/Project/Images/Home/home_item_problems.png",width: 20,height: 20),
                            const SizedBox(width: 5),
                            Text(i==0?"待办(99+)":"一星业务员"),
                          ],
                        )
                      ),
                      Container(
                        child:Row(
                          children: [
                            Image.asset("lib/Project/Images/Home/home_go_go.png",width: 20,height: 20),
                            const SizedBox(width: 10),
                          ],
                        )
                      ),
                    ],
                  ),
                )
              ],
            )
          ),
          //邀请客户得奖励
          Container(
            height: 88,
            margin: const EdgeInsets.only(top: 8,left: 12,right: 12,bottom: 8),
            child: HomeCarouseiImgView(),
          )
        ],
        )
      )
     );
  }
/*
  bool isNotchScreen(){
  
    return ScreenUtil.statusBarHeight > 20.0;
  }
  */
  void disposeItemClickAction(String title,BuildContext buildContex) {
    if (title == "待签收") {
      Navigator.of(buildContex).pushNamed('/SignBaseOrder',arguments:'123');
    } else if(title == "分发扫描") {
      Navigator.push(buildContex, MaterialPageRoute(builder: (buildContex){
        return DisScanPageDemo();
      }));
    } else if(title == "签收扫描") {
      Navigator.push(buildContex, MaterialPageRoute(builder: (buildContex){
        return SignScanPageDemo();
      },settings: RouteSettings(arguments: '我是首页界面来的')));
    } else {
      Fluttertoast.showToast(msg: "暂未配置路由",gravity:ToastGravity.CENTER);
    }
    
  }
}